import * as Blockly from 'blockly';
// 添加一个新的同步日志块定义
export const textBlocks = {
  syncLog: {
    type: 'sync_log',
    message0: '同步输出日志 %1',
    args0: [
      {
        type: 'input_value',
        name: 'TEXT',
        check: null, // 允许任何类型的输入
      },
    ],
    previousStatement: null,
    nextStatement: null,
    colour: 160,
    tooltip: '同步输出内容到控制台',
    helpUrl: '',
  }
};

// 注册文本块
let syncLogBlocksRegistered = false;

// 注册文本块
export function registerSyncLogBlocks() {
  // 如果已经注册过，则直接返回
  if (syncLogBlocksRegistered) {
    console.log('Sync log blocks already registered, skipping registration');
    return;
  }
  
  try {
    // 检查块是否已经存在
    if (Blockly.Blocks['sync_log']) {
      console.log('Block sync_log already exists, skipping definition');
    } else {
      // 注册块
      Blockly.common.defineBlocks(
        Blockly.common.createBlockDefinitionsFromJsonArray([
          textBlocks.syncLog
        ])
      );
      console.log('Sync log blocks registered successfully');
    }
    
    // 标记已经注册
    syncLogBlocksRegistered = true;
  } catch (error) {
    console.error('Error registering sync log blocks:', error);
  }
}